module Dict(Dict, emptyDict, extendDict, lookupDict, removeDict, makeDict, extendMDict) where



-- Implementación de diccionarios usando funciones.

data Dict key val = DD (key -> Maybe val)

-- *** Ejercicio 1 ***

emptyDict :: Dict key val
emptyDict = DD (\x -> Nothing)

extendDict :: Eq key => Dict key val -> key -> val -> Dict key val
extendDict (DD f) k v = DD (\ky -> if (ky == k) then (Just v) else f ky )

lookupDict :: Dict key val -> key -> Maybe val
lookupDict (DD f) k = f k

removeDict :: Eq key => Dict key val -> key -> Dict key val
removeDict (DD f) k = DD (\ky -> if (ky == k) then Nothing else f ky )

makeDict :: Eq key => [(key, val)] -> Dict key val
makeDict = foldr  (\(k,v) dc -> extendDict dc k v) emptyDict  

-- Funcion auxiliar sobre diccionario permite extender de a muchas claves.

extendMDict :: Eq key => Dict key val -> [(key, val)]  -> Dict key val
extendMDict dic = foldr  (\(k,v) dc -> extendDict dc k v) dic
